package com.yun.zf.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yun.zf.common.admin.domain.SysMenu;
import com.yun.zf.common.admin.model.sys.MenuSearchMo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 菜单表 数据层
 * 
 * @author ruoyi
 */
@Mapper
public interface SysMenuMapper extends BaseMapper<SysMenu>
{
    /**
     * 查询系统菜单列表
     * 
     * @param menu 菜单信息
     * @return 菜单列表
     */
    public List<SysMenu> selectMenuList(SysMenu menu);

    /**
     * 根据用户所有权限
     * 
     * @return 权限列表
     */
    public List<String> selectMenuPerms();

    /**
     * 根据用户查询系统菜单列表
     * 
     * @param menu 菜单信息
     * @return 菜单列表
     */

    @Select("<script>" +
            "SELECT DISTINCT m.* from sys_user u, sys_user_role ur, sys_role r   ,sys_role_menu rm,sys_menu m WHERE m.menu_id=rm.menu_id  and ur.role_id =r.role_id and rm.role_id =ur.role_id and ur.user_id=u.user_id and u.user_id= #{userId} " +
            " <if test=\"menu.menuName != null and menu.menuName != '' \">and  m.menu_name = #{menu.menuName} </if> " +
            " <if test=\"menu.status != null and menu.status != '' \">and  m.status = #{menu.status} </if>"+
            "</script>")
    public List<SysMenu> selectMenuListByUserId(@Param("menu")MenuSearchMo menu, Long userId);


    /**
     * 新增或者编辑只查询目录和菜单级别的
     * @param userId
     * @return
     */
    @Select("<script>" +
            "SELECT DISTINCT m.* from sys_user u, sys_user_role ur, sys_role r   ,sys_role_menu rm,sys_menu m WHERE m.menu_id=rm.menu_id and m.menu_type in ('M','C') and r.status='0' and ur.role_id =r.role_id and rm.role_id =ur.role_id and ur.user_id=u.user_id and u.user_id= #{userId} "+
            "</script>")

    public List<SysMenu> selectDeepTwoByUserId(Long userId);

    /**
     * 根据用户ID查询权限
     * 
     * @param userId 用户ID
     * @return 权限列表
     */
    @Select("<script>" +
            "select distinct m.perms " +
            " from  sys_role r  " +
            ", sys_role_menu rm " +
            ", sys_menu m  " +
            "where r.role_id= rm.role_id and rm.menu_id = m.menu_id and   m.status = '0' and r.status = '0' and m.perms  is not NULL  and r.role_id in  " +
            "   <foreach item='item' index='index' collection='roleIdList' open='(' separator=',' close=')' >" +
            "       #{item}" +
            "   </foreach>" +
            " ORDER BY m.perms ASC "+
            "</script>"
    )
    public List<String> selectMenuPermsByUserId(@Param("roleIdList") List<Long> roleIdList);

    /**
     * 根据用户ID查询菜单
     * 
     * @return 菜单列表
     */
    @Select("<script> " +
            "select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.menu_type, m.icon, m.order_num, m.create_time" +
            " from sys_menu m where m.menu_type in ('M', 'C') and m.status = 0 " +
            " order by m.parent_id, m.order_num " +
            " </script>")
    public List<SysMenu> selectMenuTreeAll();

    /**
     * 根据用户ID查询菜单
     * 
     * @param username 用户ID
     * @return 菜单列表
     */
    @Select("<script>" +
            "SELECT DISTINCT m.* from sys_user u, sys_user_role ur, sys_role r   ,sys_role_menu rm,sys_menu m WHERE m.status=0 and m.menu_type in ('M', 'C') and m.menu_id=rm.menu_id and r.status='0' and ur.role_id =r.role_id and rm.role_id =ur.role_id and ur.user_id=u.user_id and u.user_id=#{userId} ORDER BY m.parent_id, m.order_num "+
            "</script>")
    public List<SysMenu> selectMenuTreeByUserId(Long userId);

    /**
     * 根据角色ID查询菜单树信息
     * 
     * @param roleId 角色ID
     * @return 选中菜单列表
     */
    public List<Integer> selectMenuListByRoleId(Long roleId);




}
